package org.layuiframework.generator.config.po;

import com.baomidou.mybatisplus.generator.config.po.TableField;
import com.baomidou.mybatisplus.generator.config.rules.IColumnType;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
import org.layuiframework.generator.engine.TableFieldConverters;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Data
public class LayuiTableField {

    private boolean convert;
    private boolean keyFlag;
    private boolean keyIdentityFlag;
    private String name;
    private String type;
    private String propertyName;
    @JsonIgnore
    private IColumnType columnType;
    private String comment;
    private String fill;
    private boolean keyWords;
    private String columnName;
    @JsonIgnore
    private Map<String, Object> customMap;

    private boolean idColumn = false;
    private boolean nameColumn = false;
    private boolean verifyColumn = false;
    private boolean timeColumn = false;
    private boolean radioColumn = false;
    private boolean selectColumn = false;
    private boolean layuiMapper = false;
    @JsonIgnore
    private List<LayuiMapper> source;
    private String unit;
    private String alias;

    private String propertyType;
    private String lowerCaseName;

    public LayuiTableField () {}

    public LayuiTableField (TableField tableField) {
        this.init(tableField);
    }

    private void init(TableField tableField) {
        this.setConvert(tableField.isConvert());
        this.setKeyFlag(tableField.isKeyFlag());
        this.setKeyIdentityFlag(tableField.isKeyIdentityFlag());
        this.setName(tableField.getName());
        this.setType(tableField.getType());
        this.setPropertyName(tableField.getPropertyName());
        this.setColumnName(tableField.getColumnName());
        this.setColumnType(tableField.getColumnType());
        this.setComment(tableField.getComment());
        this.setFill(tableField.getFill());
        this.setKeyWords(tableField.isKeyWords());
        this.setCustomMap(tableField.getCustomMap());
        // TODO 如果是时间，直接设置成时间
        if("Date".equals(tableField.getPropertyType())) this.setTimeColumn(true);
        this.convertComment();
        // 列类型
        this.propertyType = null != this.columnType ? this.columnType.getType() : null;
        // 小写名字，在logstash匹配字段时使用这个匹配
        this.lowerCaseName = this.propertyName.toLowerCase();
    }

    // 没有就报错
    @JsonIgnore
    public String getAnnotationColumnName() {
        return this.keyWords && this.columnName.startsWith("\"") ? String.format("\\\"%s\\\"", this.name) : this.columnName;
    }

    public String getPropertyType() {
        return this.propertyType;
    }

    @JsonIgnore
    public String getCapitalName() {
        if (this.propertyName.length() == 1) {
            return this.propertyName.toUpperCase();
        } else {
            return Character.isLowerCase(this.propertyName.charAt(1)) ? Character.toUpperCase(this.propertyName.charAt(0)) + this.propertyName.substring(1) : this.propertyName;
        }
    }

    /**
     * 检查注释里面有没有以英文的小括号结尾，并取出里面的信息
     * 1. 如果是id 就指定这一列作为前端layui的id
     * 2. 如果是name 就指定这一列作为前端layui的name
     * 3. 如果是以 verify 打头，就说明这个信息是需要前端验证的，添加必填项验证
     * 4. 如果是以 radio: 开头,后面跟着 key.value;key.value的说明是声明一个单选项
     * 5. 在4 中不填 radio: 开头说明是一个下拉选择框
     * 6. 如果是time 就指定这一列作为前端layui的查询时间列
     * 7. 其它的作为单位
     */
    private void convertComment(){
        final String N_PLUS_PATTREN = "^[^\\(|^\\)]+(\\(([^\\(|^\\)]+)\\))?$";
        Pattern pattern = Pattern.compile(N_PLUS_PATTREN);
        Matcher matcher = pattern.matcher(this.getComment());
        String finalStr = matcher.find() ? matcher.group(matcher.groupCount()) : "";
        if(!StringUtils.hasLength(finalStr)){
            this.setAlias(StringUtils.hasLength(this.getComment()) ? this.getComment() : this.getName());
            return;
        }
        this.setAlias(this.getComment().substring(0, this.getComment().indexOf("(")));
        for (String comment: finalStr.split(",")) {
            TableFieldConverters.convert(this,comment);
        }
    }

    public static void main(String[] args) {
        String str = "洒洒水(asdasd,asdasdsadw ";
        String [] strs = str.split(",");
        System.out.println(strs[0]);
        System.out.println(strs[1]);
        str = str.substring(0, str.lastIndexOf("("));
        System.out.println("str = " + str);
        //System.getProperty("user.dir")
        System.out.println(System.getProperty("user.dir"));
    }

}
